<?php

namespace app\shop\controller\business;

use think\Controller;
use think\Request;

class Base extends Controller
{
    protected $BusinessModel = null;
    // 构造函数
    public function __construct()
    {
        // 手动继承父级的构造函数
        parent::__construct();

        // 加载模型
        $this->BusinessModel = model('business.Business');
    }

    //注册页面
    public function register()
    {
        if ($this->request->param()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            if (empty($password)) {
                $this->error('密码为空');
            }

            $salt = build_randstr();

            $password = md5($password . $salt);

            $sourceid = model('business.Source')->where(['name' => ['LIKE', '%商城%']])->value('id');

            $data = [
                'mobile' => $mobile,
                'nickname' => build_encrypt($mobile, 3, 4),
                'salt' => $salt,
                'password' => $password,
                'sourceid' => $sourceid
            ];

            $result = $this->BusinessModel->validate('common/business/Business')->save($data);

            if ($result === false) {
                $this->error($this->BusinessModel->getError());
            } else {
                $this->success('注册成功');
            }
        }
    }


    //登录页面
    public function login()
    {
        if ($this->request->param()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            // 查询数据表是否有该用户
            $business = $this->BusinessModel->where(['mobile' => $mobile])->find();

            if (!$business) {
                $this->error('该手机未注册');
            }

            // 验证密码是否正确
            $password = md5($password . $business['salt']);

            if ($password != $business['password']) {
                $this->error('密码错误');
            }

            $data = [
                'id' => $business['id'],
                'mobile' => $business['mobile'],
                'nickname' => $business['nickname'],
                'email' => $business['email'],
                'gender' => $business['gender'],
                // 'gender_text' => $business['gender_text'],
                'province' => $business['province'],
                'city' => $business['city'],
                'district' => $business['district'],
                'region_text' => $business['region_text'],
                'auth' => $business['auth'],
                'avatar_cdn' => $business['avatar_cdn'],
                'createtime_text' => $business['createtime_text']
            ];

            // cookie('business', $data);
            $this->success('登录成功', null, $data);
        }
    }

    public function check()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'trim');
            $mobile = $this->request->param('mobile', '', 'trim');

            $where = [
                'id' => $id,
                'mobile' => $mobile
            ];

            $business = $this->BusinessModel->where($where)->find();

            if (!$business) {
                $this->error('用户不存在');
            } else {

                // 返回前端
                $data = [
                    'id' => $business['id'],
                    'mobile' => $business['mobile'],
                    'nickname' => $business['nickname'],
                    'email' => $business['email'],
                    'gender' => $business['gender'],
                    'gender_text' => $business['gender_text'],
                    'province' => $business['province'],
                    'city' => $business['city'],
                    'district' => $business['district'],
                    'region_text' => $business['region_text'],
                    'auth' => $business['auth'],
                    'avatar_cdn' => $business['avatar_cdn'],
                    'createtime_text' => $business['createtime_text']
                ];

                $this->success('验证登录成功', null, $data);
            }
        }
    }

    //更新操作
    public function profile()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();

            $business = $this->BusinessModel->find($params['id']);

            if (!$business) {
                $this->error('用户不存在');
            }

            $code = $params['district'] ? $params['district'] : ($params['city'] ? $params['city'] : $params['province']);

            $path = model('Region')->where(['code' => $code])->find();

            if (!$path) {
                $this->error('所选地区不存在');
            }

            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {          //$_FILES()预定义数组变量，用于接收上传到服务器上的文件数据
                $avatar = build_upload('avatar');  //文件上传

                if ($avatar['code'] === 0) {
                    $this->error($avatar['msg']);
                }

                $params['avatar'] = $avatar['data'];
            }

            if (!empty($params['password'])) {
                $repass = md5($params['password'] . $business['salt']);

                if ($repass === $business['password']) {
                    $this->error('原密码与新密码一致');
                }

                $salt = build_randstr();

                $params['salt'] = $salt;
                $params['password'] = md5($params['password'] . $salt);
            }

            $result = $this->BusinessModel->validate('common/business/Business.profile')->isUpdate(true)->save($params);
           
            if ($result === false) {
                if (isset($params['avatar']) && $_FILES['avatar']['size'] > 0) {
                    is_file('.' . $params['avatar']) && unlink('.' . $params['avatar']);
                }

                $this->error($this->BusinessModel->getError());
            } else {
                if (isset($params['avatar']) && $_FILES['avatar']['size'] > 0) {
                    is_file('.' . $business['avatar']) && unlink('.' . $business['avatar']);
                }

                $business = $this->BusinessModel->find($params['id']);

                $data = [
                    'id' => $business['id'],
                    'mobile' => $business['mobile'],
                    'nickname' => $business['nickname'],
                    'email' => $business['email'],
                    'gender' => $business['gender'],
                    'gender_text' => $business['gender_text'],
                    'province' => $business['province'],
                    'city' => $business['city'],
                    'district' => $business['district'],
                    'region_text' => $business['region_text'],
                    'auth' => $business['auth'],
                    'avatar_cdn' => $business['avatar_cdn'],
                    'createtime_text' => $business['createtime_text']
                ];

                $this->success('更新基本资料成功', null, $data);
            }
        }
    }
}
